<?php
namespace app\serverrecommend\logic;

use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-09-28 17:34:29
 * @Description: 推荐位逻辑层
 */
class Recommend extends BaseLogic
{
    
	protected $name = 'recommend';
    protected $config = 'db_config_recommend';
    protected $recommendLogic = '\app\serverrecommend\logic\Recommend';
    protected $recommendModel = '\app\serverrecommend\model\Recommend';
    

    use \app\serveruser\traits\addAppId;
    use \app\servergoods\traits\UpdateCate;
    use \app\servergoods\traits\GetCateTree;
    use \app\servergoods\traits\OperateAppCate;
    
    /*检验识别码的唯一性*/
    public function checkAction($data): array 
    {
        $info = $this->db()->where(['action'=>$data['action']])->find();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }

    /*内部获取某个分类的详情*/
    public function dbCateInfo($data) 
    {
        return $this->db()->where(['id'=>$data['id'],'status'=>['neq','-1']])->find();
    }

    /*内部获取不包含公共分类的所有分类列表 查询结果可用于操作*/
    public function dbCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        isset($map['app_id']) && $_where['app_id'] = $map['app_id'];
        $_where['status'] = ['neq',-1];
        return $this->db()->where($_where)->order('`order` ASC, id DESC')->select();
    }

    /*内部获取包含公共分类的所有分类列表  仅查询不用于操作*/
    public function dbAllCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        $_where['status'] = $map['status'] ?? ['neq',-1];
        if(isset($map['app_id'])){
            $isManageAppId = $this->_isManageAppId($map['app_id']);
            $_whereOr = $isManageAppId ? true : "`app_id` = {$map['app_id']} OR FIND_IN_SET({$map['app_id']},`other_app_read`) OR is_public = 1";
        }else{
            $_whereOr = true;
        }
        if(isset($map['limit']) && $map['limit']){
            $res = $this->db()->where($_where)->where($_whereOr)->order('`order` ASC, id DESC')->limit($map['limit'])->select();
        }else{
            $res = $this->db()->where($_where)->where($_whereOr)->order('`order` ASC, id DESC')->select();
        }
        return $res;
    }

    //添加推荐位分类
    public function addRecommend($data): array 
    {
        $res = $this->model($this->recommendModel)->save($data);
        return auto_return($res, '添加分类成功', '添加分类失败');
    }

    /*编辑某个推荐位*/
    public function editRecommend($data): array 
    {
        return $this->_updateCate($this->recommendLogic, $this->recommendModel, $data);
    }

    /*后台获取本应用下的分类树 包含公共分类 包含除“删除”外的所有分类*/
    public function getTree($data): array 
    {
        return $this->_getCateTree($data, $this->recommendLogic, 'dbAllCateList');
    }

    /*获取某个不删除的分类详情*/
    public function oneCateInfo($data): array 
    {
        $map['status'] = ['neq', -1];
        return $this->_oneCateInfo($data, $this->model($this->recommendModel), $map);
    }

    /*后台删除某个分类*/
    public function delOneCate($data): array 
    {
        return $this->_delOneCate($data, $this->model($this->recommendModel));
    }

    /*前台获取action所属分类下的子类 多级*/
    public function getChildByAction($data): array 
    {
        //获取该分类的id
        $_where['action'] = $data['action'];
        $_where['status'] = 1;
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        $actionInfo = $this->model($this->recommendModel)->where($_where)->where($_whereOr)->find();
        if(!$actionInfo){
            return null_data(null, '该分类不存在');
        }
        $_getChild['status'] = 1;
        $_getChild['app_id'] = $data['app_id'];
        $list = $this->dbAllCateList($_getChild);
        $list = list_to_tree($list,'id','pid','_child',$actionInfo['id']);

        return auto_return($list, '成功获取分类列表', '获取分类列表失败');
    }

    /*前台获取action所属分类下的子类 一级*/
    public function getOneLevChildByAction($data): array 
    {
        //获取该分类的id
        $_where['action'] = $data['action'];
        $_where['status'] = 1;
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        $actionInfo = $this->model($this->recommendModel)->where($_where)->where($_whereOr)->find();
        if(!$actionInfo){
            return null_data(null, '该分类不存在');
        }
        $_getChild['status'] = 1;
        $_getChild['pid'] = $actionInfo['id'];
        $_getChild['app_id'] = $data['app_id'];
        $_getChild['limit'] = $data['limit'];
        $list = $this->dbAllCateList($_getChild);

        return auto_return($list, '成功获取分类列表', '获取分类列表失败');
    }


}
